<%+cbi/valueheader%>

<%
	require("uci")
	require("luci.sys")

	local fs = require "nixio.fs"

	function string_split(str, split_char)
	    local sub_str_tab = {}
	    local i = 0
	    local j = 0

	    while true do
		j = string.find(str, split_char, i + 1)
		if j == nil then
		    table.insert(sub_str_tab, str)
		    break
		end
		table.insert(sub_str_tab, string.sub(str, i + 1, j - 1))
		i = j
	    end

	    return sub_str_tab
	end

	function cups_get_port()
		local cfg = string_split(fs.readfile("/etc/cups/cupsd.conf"), "\n")
		local port_num = 631

		for i = 1, #(cfg) do
			if string.find(cfg[i], "Port ") == 1 then
				port_num = string.match(cfg[i], "^Port *(%d+)")
				break
			end
		end

		return port_num
	end

	local x = uci.cursor()

	ipaddr_lan = x:get("network", "lan", "ipaddr")
	port_cups = cups_get_port();
%>

<input class="cbi-button cbi-input-save" id="" type="button" value="<%:Open CUPS management%>" onclick="window.open('http://<%=ipaddr_lan%>:<%=port_cups%>/')" />


